import { _decorator, Component, Node, log, screen, v3, view, UITransform, Enum } from 'cc';
const { ccclass, property } = _decorator;

/**
 * 适配屏幕高度的情况下适用，
 * 拉伸（可以选择维持宽高比）至全屏幕大小，以解决窄于屏幕壁纸的两侧黑边，也可使壁纸跨越安全区外
 */

export enum FixType {
    FitHeight,
    FitWidth,
}
Enum(FixType);

export enum ResizeMode {
    Stretch,
    FixAspect
}
Enum(ResizeMode);

enum LifeCycleFunc {
    ON_LOAD,
    START,
}
Enum(LifeCycleFunc)

@ccclass('ResizeToVisibleSize')
export class ResizeToVisibleSize extends Component {

    @property({ type: FixType })
    fixType: FixType = FixType.FitHeight;

    @property({ type: ResizeMode })
    resizeMode: ResizeMode = ResizeMode.Stretch;

    @property({ type: LifeCycleFunc })
    lifeCycleFunc: LifeCycleFunc = LifeCycleFunc.START;

    @property
    exRate = 1;

    protected onLoad(): void {
        if (this.lifeCycleFunc == LifeCycleFunc.ON_LOAD) this.go();
    }

    protected start(): void {
        if (this.lifeCycleFunc == LifeCycleFunc.START) this.go();
    }

    go() {
        const size = view.getVisibleSize();
        const winSize = screen.windowSize;

        size.height = size.width * winSize.height / winSize.width;

        const designSize = view.getDesignResolutionSize();

        if (this.fixType == FixType.FitHeight) {
            let k = size.width / designSize.width;
            if (this.resizeMode == ResizeMode.Stretch) {
                if (k > 1) { // 宽较大，如果是背景图片的话，需要放大
                    this.node.getComponent(UITransform).width *= k * this.exRate;
                    this.node.getComponent(UITransform).height *= this.exRate;
                } else {
                    this.node.getComponent(UITransform).width *= this.exRate;
                    this.node.getComponent(UITransform).height *= this.exRate;
                }
            } else if (this.resizeMode == ResizeMode.FixAspect) {
                if (k > 1) { // 宽较大，如果是背景图片的话，需要放大
                    this.node.getComponent(UITransform).width *= k * this.exRate;
                    this.node.getComponent(UITransform).height *= k * this.exRate;
                } else {
                    this.node.getComponent(UITransform).width *= this.exRate;
                    this.node.getComponent(UITransform).height *= this.exRate;
                }
            }
        } else if (this.fixType == FixType.FitWidth) {
            let k = size.height / designSize.height;
            if (this.resizeMode == ResizeMode.Stretch) {
                if (k > 1) {
                    this.node.getComponent(UITransform).width *= this.exRate;
                    this.node.getComponent(UITransform).height *= k * this.exRate;
                } else {
                    this.node.getComponent(UITransform).width *= this.exRate;
                    this.node.getComponent(UITransform).height *= this.exRate;
                }
            } else if (this.resizeMode == ResizeMode.FixAspect) {
                if (k > 1) {
                    this.node.getComponent(UITransform).width *= k * this.exRate;
                    this.node.getComponent(UITransform).height *= k * this.exRate;
                } else {
                    this.node.getComponent(UITransform).width *= this.exRate;
                    this.node.getComponent(UITransform).height *= this.exRate;
                }
            }
        }

    }
}


